package com.pshcp.service;

import cn.hutool.core.collection.CollUtil;
import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.framework.util.SystemUtil;
import com.github.pagehelper.PageHelper;
import com.pshcp.common.enums.CommonStatusEnum;
import com.pshcp.common.enums.NewsTypeEnum;
import com.pshcp.domain.dto.NewsDTO;
import com.pshcp.domain.model.News;
import com.pshcp.mapper.NewsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2022-11-24
 */
@Service
public class NewsService {

    @Autowired
    private NewsMapper newsMapper;

    public PageList<News> list(NewsDTO newsDTO){
        if(newsDTO.getPageNum() != null && newsDTO.getPageSize() != null){
            PageHelper.startPage(newsDTO.getPageNum(), newsDTO.getPageSize());
        }
        Example example = new Example(News.class);
        Example.Criteria criteria = example.createCriteria();
        if(newsDTO.getStatus() != null){
            criteria.andEqualTo("status", newsDTO.getStatus());
        }else{
            criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        }
        example.setOrderByClause(" created_time desc");
        List<News> list = newsMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public PageList<News> pageList(NewsDTO newsDTO){
        if(newsDTO.getPageNum() != null && newsDTO.getPageSize() != null){
            PageHelper.startPage(newsDTO.getPageNum(), newsDTO.getPageSize());
        }
        Example example = new Example(News.class);
        Example.Criteria criteria = example.createCriteria();
        if(newsDTO.getStatus() != null){
            criteria.andEqualTo("status", newsDTO.getStatus());
        }else{
            criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        }
        if(newsDTO.getType() != null){
            criteria.andEqualTo("type", newsDTO.getType());
        }
        example.setOrderByClause(" created_time desc");
        example.excludeProperties("content");
        List<News> list = newsMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public List<News> select(News news){
        return newsMapper.select(news);
    }

    public int insert(News news){
        news.setId(SystemUtil.getId());
        news.setStatus(CommonStatusEnum.INIT.getCode());
        news.setCreatedTime(new Date());
        return newsMapper.insert(news);
    }

    public int batchInsert(List<News> list){
        list.forEach(news -> {
            news.setId(SystemUtil.getId());
            news.setStatus(CommonStatusEnum.INIT.getCode());
            news.setCreatedTime(new Date());
        });
        return newsMapper.batchInsert(list);
    }

    public int update(News news){
        news.setUpdateTime(new Date());
        return newsMapper.updateByPrimaryKeySelective(news);
    }

    public News findById(Long id){
        return newsMapper.selectByPrimaryKey(id);
    }

    public List<News> findByIds(List<Long> ids){
        Example example = new Example(News.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return newsMapper.selectByExample(example);
    }

    public int delById(Long id){
        Example example = new Example(News.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        News news = new News();
        news.setStatus(CommonStatusEnum.DEL.getCode());
        return newsMapper.updateByExampleSelective(news, example);
    }

    /**
     * 更新新闻
     * @param list
     * @param minDay 更新新闻的最前日期
     */
    @Transactional(rollbackFor = Throwable.class)
    public void updateNews(List<News> list, LocalDate minDay, NewsTypeEnum type){
        delByGeDay(minDay, type);
        List<List<News>> batch = CollUtil.split(list,100);
        batch.forEach(item -> batchInsert(item));
    }

    /**
     * 根据日期删除新闻
     * ge表示 >=
     * @param day 含当前日期
     * @return
     */
    public int delByGeDay(LocalDate day, NewsTypeEnum type){
        return newsMapper.delByGeDay(day, type.getCode());
    }

}
